/**
 * Copyright 2009 University of  Washington
 * This code is released under the GPL version 3 - with a copy of the license included
 * @author Maya Hao Li
 *
 */
package seedpod.webapp.view.htmlwidget;

public class ImageButton extends GenericHtmlWidget {
	private static String CSS_CLASS = "imageButton";
	private String _imageSrc = "", _href = "", _alt = "";
	private String _javascript = "";
	private String _jsResource = "";
	private String _align = "middle";
	private boolean _active = true;

	public ImageButton() {
		_imageSrc = "";
		setCssClass( CSS_CLASS );
		_widgetID = "imageButton";
	}

	public ImageButton(String widgetID, String imageSrc, String href, String alt) {
		_widgetID = widgetID;
		_imageSrc = imageSrc;
		_href = href;
		_alt = alt;
		_cssClass = CSS_CLASS;
	}

	public void addJavascript( String resource, String js ) {
		_jsResource = resource;
		_javascript += js;
	}

	public void addJavascript( String js ) {
		_javascript = js;
	}

	public void setAlign( String align ) {
		this._align = align;
	}

	public void inactivate() {
		_active = false;
	}

	public String render() {
		String html = "";
		if ( _javascript.length() > 0 && _jsResource.length() > 0 )
			html += "<script language=\"javascript\" " + ATT( "src", _jsResource ) + "></script>";

		if ( _active ) {
			if ( _javascript.length() > 0 ) {
				html += "<input " 
						+ ATT( "type", "image" ) 
						+ ATT( "name", _widgetID )
						+ ATT( "src", _imageSrc ) 
						+ ATT( "class", "imageButton" ) 
						+ ATT( "alt", _alt )
						+ ATT( "align", _align) 
						+ ATT("id", _widgetID) ;
				html += _javascript;
				html += " >";
			} else {
				html += "<a " 
						+ ATT( "name", _widgetID ) 
						+ ATT( "href", _href ) 
						+ ATT( "alt", _alt )
						+ ATT( "align", _align ) 
						+ ATT("id", _widgetID) + ">";
				html += "<img " 
						+ ATT( "src", _imageSrc ) 
						+ ATT( "class", "imageButton" )
						+ ATT( "alt", _alt ) 
						+ ATT( "border", 0 ) 
						+ ATT( "align", _align ) + ">";
				html += "</a>";
			}
		} else {
			html += "<img " 
						+ ATT( "src", _imageSrc ) 
						+ ATT( "class", "inactiveImageButton" )
						+ ATT( "alt", _alt ) 
						+ ATT( "border", 0 ) 
						+ ATT( "align", _align ) 
						+ ATT("id", _widgetID) + ">";
		}
		return html;
	}

}